<?php

class Processdesigner_model extends Model
{
    function __construct()
    {
        parent::Model();
    }

    function insert_process($process_name, $process_description,
                            $process_admin, $process_owners=array())
    {
        $data = array(
                'process_name'        => $process_name,
                'process_description' => $process_description
        );

        $this->db->insert('processes', $data);

        $process_id = $this->db->insert_id();

        $data = array(
                'process_id'          => $process_id,
                'user_id'             => $process_admin,
                'process_owner_level' => PROCESS_ADMIN
        );

        $this->db->insert('process_owners', $data);

        foreach($process_owners as $user_id)
        {
            $data = array(
                'process_id'          => $process_id,
                'user_id'             => $user_id,
                'process_owner_level' => PROCESS_OWNER
            );

            $this->db->insert('process_owners', $data);
        }
        return $process_id;
    }

    function update_process($process_id, $process_name, $process_description,
                            $process_admin, $process_owners=array())
    {
        $data = array(
            'process_name'        => $process_name,
            'process_description' => $process_description
        );
        $this->db->update('processes', $data, array('process_id' => $process_id));

        $where = array(
            'process_id' => $process_id,
            'process_owner_level' => PROCESS_ADMIN
        );
        $this->db->update('process_owners', array('user_id' => $process_admin), $where);

        $where['process_owner_level'] = PROCESS_OWNER;
        $this->db->delete('process_owners', $where);

        foreach($process_owners as $process_owner)
        {
            $data = array(
                'process_id'          => $process_id,
                'user_id'             => $process_owner,
                'process_owner_level' => PROCESS_OWNER
            );

            $this->db->insert('process_owners', $data);
        }
    }

    function insert_stage($process_id, $stage_name, $stage_description, $previous_stages,
                          $join_type, $split_type, $stage_admin, $stage_participants)
    {
        $data = array(
            'process_id'        => $process_id,
            'stage_name'        => $stage_name,
            'stage_description' => $stage_description,
            'join_type'         => "b '".$join_type."'",
            'split_type'        => "b '".$split_type."'"
        );

        if($join_type == null)
        {
            $data['join_type'] = null;
        }

        if($split_type == null)
        {
            $data['split_type'] = null;
        }


        $this->db->insert('stages', $data);

        $stage_id = $this->db->insert_id();

        foreach($previous_stages as $previous_stage)
        {
            $data = array(
                'stage_id'          => $stage_id,
                'previous_stage_id' => $previous_stage
            );

            $this->db->insert('stage_rules', $data);
        }

        $data = array(
            'stage_id'                => $stage_id,
            'user_id'                 => $stage_admin,
            'stage_participant_level' => STAGE_ADMIN
        );

        $this->db->insert('stage_participants', $data);

        foreach($stage_participants as $stage_participant)
        {
            $data = array(
                'stage_id'                => $stage_id,
                'user_id'                 => $stage_participant,
                'stage_participant_level' => STAGE_PARTICIPANT
            );

            $this->db->insert('stage_participants', $data);
        }

        return $stage_id;
    }

    function update_stage($stage_id, $process_id, $stage_name, $stage_description, $previous_stages,
                          $join_type, $split_type, $stage_admin, $stage_participants)
    {
        $data = array(
            'process_id'        => $process_id,
            'stage_name'        => $stage_name,
            'stage_description' => $stage_description,
            'join_type'         => "b '".$join_type."'",
            'split_type'        => "b '".$split_type."'"
        );

        if($join_type == null)
        {
            $data['join_type'] = null;
        }

        if($split_type == null)
        {
            $data['split_type'] = null;
        }

        $this->db->update('stages', $data, array('stage_id' => $stage_id));

        $this->db->delete('stage_rules', array('stage_id' => $stage_id));

        foreach($previous_stages as $previous_stage)
        {
            $data = array(
                'stage_id' => $stage_id,
                'previous_stage_id' => $previous_stage
            );
            $this->db->insert('stage_rules', $data);
        }

        $where = array(
            'stage_id'                => $stage_id,
            'stage_participant_level' => STAGE_ADMIN
        );

       $this->db->update('stage_participants', array('user_id' => $stage_admin), $where);

       $where['stage_participant_level'] = STAGE_PARTICIPANT;
       $this->db->delete('stage_participants', $where);

       foreach($stage_participants as $stage_participant)
       {
           $data = array(
                'stage_id'                => $stage_id,
                'user_id'                 => $stage_participant,
                'stage_participant_level' => STAGE_PARTICIPANT
            );

            $this->db->insert('stage_participants', $data);
       }
    }

    function insert_contribution($stage_id, $contribution_title, $contribution_description)
    {
        $data = array(
            'stage_id'           => $stage_id,
            'contribution_title' => $contribution_title,
            'contribution_description'        => $contribution_description
        );

        $this->db->insert('contributions', $data);

        return $this->db->insert_id();
    }

    function update_contribution($contribution_id, $stage_id, $contribution_title, $contribution_description)
    {
        $data = array(
            'stage_id'                         => $stage_id,
            'contribution_title'               => $contribution_title,
            'contribution_description'         => $contribution_description
        );

        $this->db->update('contributions', $data, array('contribution_id' => $contribution_id));
    }

    function insert_attribute($contribution_id, $attribute_name, $attribute_description, $attribute_type, $optional)
    {
        $data = array(
            'contribution_id'      => $contribution_id,
            'attribute_name'       => $attribute_name,
            'attribute_description' => $attribute_description,
            'attribute_type'       => $attribute_type,
            'optional'             => $optional
        );

        $this->db->insert('attributes', $data);

        return $this->db->insert_id();
    }

    function update_attribute($attribute_id, $contribution_id, $attribute_name, $attribute_description, $attribute_type, $optional)
    {
        $data = array(
            'contribution_id'      => $contribution_id,
            'attribute_name'       => $attribute_name,
            'attribute_description' => $attribute_description,
            'attribute_type'       => $attribute_type,
            'optional'             => $optional
        );

        $this->db->update('attributes', $data, array('attribute_id' => $attribute_id));
    }

    function get_processdata($process_id)
    {
        $this->db->select('*');
        $this->db->from('processes');
        $this->db->where('process_id', $process_id);

        $query = $this->db->get();

        if($query->num_rows() > 0)
        {
            return $query->row_array();
        }
        else
        {
            return FALSE;
        }
    }

    function get_process_owners($process_id)
    {
        $query = $this->db->get_where('process_owners', array('process_id' => $process_id));

        return $query->result_array();
    }
    
    function get_process_admin($process_id)
    {
        $query = $this->db->get_where('process_owners', array(
            'process_id'          => $process_id,
            'process_owner_level' => PROCESS_ADMIN
         ));

        if($query->num_rows() > 0)
        {
            return $query->row_array();
        }
        else
        {
            return FALSE;
        }
    }

    function all_stages()
    {
        $query = $this->db->get('stages');
        return $query->result_array();
    }

    function get_stages($process_id)
    {
        $this->db->select('*');
        $this->db->from('stages');
        $this->db->where('process_id', $process_id);
        $this->db->order_by('stage_id');

        $query = $this->db->get();

        return $query->result_array();
    }

    function get_stagedata($stage_id)
    {
        $query = $this->db->get_where('stages', array('stage_id' => $stage_id));

        if($query->num_rows() > 0)
        {
            return $query->row_array();
        }
        else
        {
            return FALSE;
        }
    }

    function get_contributiondata($contribution_id)
    {
        $query = $this->db->get_where('contributions', array('contribution_id' => $contribution_id));

        if($query->num_rows() > 0)
        {
            return $query->row_array();
        }
        else
        {
            return FALSE;
        }
    }

    function get_attributedata($attribute_id)
    {
       $query = $this->db->get_where('attributes', array('attribute_id' => $attribute_id ));

       if($query->num_rows() > 0)
       {
           return $query->row_array();
       }
       else
       {
           return FALSE;
       }
    }

    function get_contributions($stage_id)
    {
        $this->db->select('*');
        $this->db->from('contributions');
        $this->db->where('stage_id', $stage_id);
        $this->db->order_by('contribution_id');

        $query = $this->db->get();

        return $query->result_array();
    }

    function get_attributes($contribution_id)
    {
        $this->db->select('*');
        $this->db->from('attributes');
        $this->db->where('contribution_id', $contribution_id);
        $this->db->order_by('attribute_id');

        $query = $this->db->get();

        return $query->result_array();
    }

    function is_process_owner($user_id, $process_id)
    {
        $this->db->select('*');
        $this->db->from('process_owners');
        $this->db->where('user_id', $user_id);
        $this->db->where('process_id', $process_id);

        $query = $this->db->get();

        return ($query->num_rows() > 0);
    }

    function previous_stages($stage_id)
    {
        $stage_id = $this->db->escape($stage_id);
        $select = "SELECT *
                   FROM stages
                   WHERE stage_id IN (SELECT sr.previous_stage_id
                                      FROM stage_rules as sr
                                      WHERE stage_id={$stage_id})";

        $query = $this->db->query($select);

        return $query->result_array();
    }

    function stage_participants($stage_id, $level=STAGE_PARTICIPANT)
    {
        $stage_id = $this->db->escape($stage_id);
        $select = "SELECT *
                   FROM users
                   WHERE user_id IN (SELECT sp.user_id
                                     FROM stage_participants as sp
                                     WHERE sp.stage_id={$stage_id}
                                     AND sp.stage_participant_level={$level})";

        $query = $this->db->query($select);

        return $query->result_array();
    }

    function get_owner_process($stage_id)
    {
        $stage_id = $this->db->escape($stage_id);
        $select = "SELECT *
                   FROM processes
                   WHERE process_id IN (SELECT s.process_id
                                        FROM stages as s
                                        WHERE s.stage_id = {$stage_id})";

        $query = $this->db->query($select);

        if($query->num_rows() > 0)
        {
            return $query->row_array();
        }
        else
        {
            return FALSE;
        }
    }

    function get_owner_stage($contribution_id)
    {
        $contribution_id = $this->db->escape($contribution_id);
        $select = "SELECT *
                   FROM stages
                   WHERE stage_id IN (SELECT c.stage_id
                                      FROM contributions as c
                                      WHERE c.contribution_id = {$contribution_id})";

        $query = $this->db->query($select);

        if($query->num_rows() > 0)
        {
            return $query->row_array();
        }
        else
        {
            return FALSE;
        }
    }

    function get_owner_contribution($attribute_id)
    {
        $attribute_id = $this->db->escape($attribute_id);
        $select = "SELECT *
                   FROM contributions
                   WHERE contribution_id IN (SELECT a.contribution_id
                                             FROM attributes as a
                                             WHERE a.attribute_id = {$attribute_id})";

        $query = $this->db->query($select);

        if($query->num_rows() > 0)
        {
            return $query->row_array();
        }
        else
        {
            return FALSE;
        }
    }

    function delete_process($process_id)
    {
        $where = array('process_id' => $process_id);
        $this->db->delete('processes', $where);
        $this->db->delete('process_owners', $where);
        $this->db->delete('process_instances', $where);

        $query = $this->db->get_where('stages', $where);
        $rows = $query->result_array();

        foreach($rows as $row)
        {
            $this->delete_stage($row['stage_id']);
        }
    }

    function delete_stage($stage_id)
    {
        $where = array('stage_id' => $stage_id);
        $this->db->delete('stages', $where);
        $this->db->delete('stage_instances', $where);
        $this->db->delete('stage_participants', $where);
        $this->db->delete('stage_rules', $where);
        $this->db->delete('stage_rules', array('previous_stage_id' => $stage_id));

        $query = $this->db->get_where('contributions', $where);
        $rows = $query->result_array();

        foreach($rows as $row)
        {
            $this->delete_contribution($row['contribution_id']);
        }
    }

    function delete_contribution($contribution_id)
    {
        $where = array('contribution_id' => $contribution_id);
        $this->db->delete('contributions', $where);
        $this->db->delete('contribution_instances', $where);

        $query = $this->db->get_where('attributes', $where);
        $rows = $query->result_array();

        foreach($rows as $row)
        {
            $this->delete_attribute($row['attribute_id']);
        }
    }

    function delete_attribute($attribute_id)
    {
        $where = array('attribute_id' => $attribute_id);
        $this->db->delete('attributes', $where);
        $this->db->delete('input_datas', $where);
        $this->db->delete('documents', $where);
    }

}

?>
